﻿namespace Alkitab
{
    using Alkitab.Language;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;

    public partial class HistoryAndBookmark : PhoneApplicationPage
    {
        #region Fields

        public Boolean isthishistory = false;

        #endregion Fields

        #region Constructors

        public HistoryAndBookmark()
        {
            InitializeComponent();
            BuildApplicationBar();

            historylist.FontSize = (int)Globals.Settingan["fontsize?"] - 3;
            bookmarklist.FontSize = (int)Globals.Settingan["fontsize?"] - 3;

            historylist.ItemsSource = Globals.history;
            bookmarklist.ItemsSource = Globals.bookmarks;
        }

        #endregion Constructors

        #region Methods

        private void bookmarklist_Loaded(object sender, RoutedEventArgs e)
        {
            bookmarklist.ItemsSource = null;
            bookmarklist.ItemsSource = Globals.bookmarks;
        }

        private void BuildApplicationBar()
        {
            ApplicationBar = new ApplicationBar();

            ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(Labels.clear);
            appBarMenuItem.Click += new EventHandler(clear_click);
            ApplicationBar.MenuItems.Add(appBarMenuItem);
        }

        private void clear_click(object sender, EventArgs e)
        {
            if (isthishistory)
            {
                if (MessageBox.Show(Labels.MSG_ClearHistory, Labels.confirmation, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Globals.history.Clear();
                    historylist.ItemsSource = null;
                    historylist.ItemsSource = Globals.history;
                }
            }
            else
            {
                if (MessageBox.Show(Labels.MSG_ClearBookmark, Labels.confirmation, MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Globals.bookmarks.Clear();
                    bookmarklist.ItemsSource = null;
                    bookmarklist.ItemsSource = Globals.bookmarks;
                }
            }
        }

        private void etwas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isthishistory)
            {
                if (historylist.SelectedIndex > -1)
                {
                    Globals.terjemahansblmnya = Globals.terjemahanlamakah;
                    Globals.nopasalsblmnya = Globals.nopasal;
                    Globals.nobukusblmnya = Globals.nobuku;

                    AyatFull ayat = Globals.history[historylist.SelectedIndex];
                    Globals.terjemahanlamakah = ayat.terjemahanLama;
                    Globals.nobuku = ayat.noBuku;
                    Globals.nopasal = ayat.noPasal;
                    Globals.noayat = ayat.noAyat;

                    if ((!(Globals.nobuku == 0)) && (!(Globals.nopasal == 0)))
                    {
                        Globals.stopupdatinghistory = true;
                        NavigationService.Navigate(new Uri("/View/Bacaan.xaml", UriKind.Relative));
                        historylist.SelectedIndex = -1;
                    }
                    else showMSGBug();
                }
            }
            else
            {
                if (bookmarklist.SelectedIndex > -1)
                {
                    Globals.terjemahansblmnya = Globals.terjemahanlamakah;
                    Globals.nopasalsblmnya = Globals.nopasal;
                    Globals.nobukusblmnya = Globals.nobuku;

                    AyatFull ayat = Globals.history[bookmarklist.SelectedIndex];
                    Globals.terjemahanlamakah = ayat.terjemahanLama;
                    Globals.nobuku = ayat.noBuku;
                    Globals.nopasal = ayat.noPasal;
                    Globals.noayat = ayat.noAyat;

                    if ((!(Globals.nobuku == 0)) && (!(Globals.nopasal == 0)))
                    {
                        Globals.stopupdatinghistory = true;
                        NavigationService.Navigate(new Uri("/View/Bacaan.xaml", UriKind.Relative));
                        bookmarklist.SelectedIndex = -1;
                    }
                    else
                        showMSGBug();
                }
            }
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            isthishistory = !(isthishistory);
        }

        private void remove_click(object sender, RoutedEventArgs e)
        {
            if (isthishistory)
            {
                ListBoxItem selectedListBoxItem = this.historylist.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
                int selectedNo = ((historylist.ItemsSource) as ObservableCollection<AyatFull>).IndexOf(selectedListBoxItem.Content as AyatFull);
                Globals.history.RemoveAt(selectedNo);
                historylist.ItemsSource = null;
                historylist.ItemsSource = Globals.history;
            }
            else
            {
                ListBoxItem selectedListBoxItem = this.bookmarklist.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
                int selectedNo = ((bookmarklist.ItemsSource) as ObservableCollection<AyatFull>).IndexOf(selectedListBoxItem.Content as AyatFull);
                Globals.bookmarks.RemoveAt(selectedNo);
                bookmarklist.ItemsSource = null;
                bookmarklist.ItemsSource = Globals.bookmarks;
            }
        }

        public static void showMSGBug()
        {
            MessageBox.Show(Labels.MSG_BugFound);
        }

        #endregion Methods
    }
}